--*************ANIMATION SUITE****************--
--**************Property of Hxrmn*****************--
--*************Hxrmn@comcast.net*****************--
--DO NOT RE-DISTRIBUTE WITHOUT HXRMN'S CONSENT--
--********************************************--

KeyFrame = {}

function KeyFrame:Create(t, track)
	local frame = {}
	setmetatable(frame, self)
	self.__index = self
	frame.time = t
	frame.track = track
	frame.properties = {}
	frame:SetEntity(track:GetEntity())
	frame:CreateProperties()
	
	return frame
end

function KeyFrame:CreateProperties()
	local ent = self.entity
	if(ent == nil or ent:IsValid() == false) then return end
	self:CreateEntityProperties(ent)
	local pcount = ent:GetPhysicsObjectCount()
	if(pcount > 1) then
		for i=0, pcount-1 do
			local phys = ent:GetPhysicsObjectNum(i)
			if(phys:IsValid()) then
				self:CreatePhysObjProperties(phys)
			end
		end
	else
		local phys = ent:GetPhysicsObject()
		if(phys != nil and phys:IsValid()) then
			self:CreatePhysObjProperties(phys)
		end	
	end
end

function KeyFrame:CreatePhysObjProperties(obj)
	for k,v in pairs(ParamList.PhysObj.Setters) do
		local get = ParamList.PhysObj.Getters[k]
		local set = ParamList.PhysObj.Setters[k]
		local property = Property:Create(k, obj, set, get(obj))
		self:AddProperty(property)
	end
end

function KeyFrame:CreateEntityProperties(obj)
	for k,v in pairs(ParamList.Entity.Setters) do
		local get = ParamList.Entity.Getters[k]
		local set = ParamList.Entity.Setters[k]
		local property = Property:Create(k, obj, set, get(obj))
		self:AddProperty(property)
	end
end

function KeyFrame:SetEntity(ent)
	self.entity = ent
end

function KeyFrame:AddProperty(property)
	table.insert(self.properties, property)
end

function KeyFrame:Apply()
	if(self.entity != nil and self.entity:IsValid()) then
		for k,v in pairs(self.properties) do
			v:Apply();
		end
	end
end

function KeyFrame:Clear()
	self.properties = {};
end

function KeyFrame:GetProperties()
	return self.properties
end

function KeyFrame:GetTime()
	return self.time
end